package Operations;

import Book.BookList;
import Book.Book;

import java.util.Scanner;

public class DeleteOperation implements Operation{

    @Override
    public void operation(BookList bookList) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("删除图书");

        System.out.println("输入要删除的书名");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {//判断有没有名字相同的书
            if (name .equals(bookList.getBooks()[i].getName())) {//有这本书并且书没被借出
                if (!bookList.getBooks()[i].isBorrowed()) {
                    for (int j = i; j < currentSize; j++) {
                        Book book = bookList.getBooks()[j + 1];
                        bookList.setBooks(j, book);
                    }
                    bookList.setBooks(--currentSize, null);
                    bookList.setUsedSize(currentSize);
                    System.out.println("删除成功");
                    new ShowOperation().operation(bookList);
                    return;
                } else {
                    System.out.println("书被借出，无法删除");
                    return;
                }
            }
        }

        System.out.println("没有该书");

    }
}
